package com.hubiao.pay.merchant;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Description
 *
 * @author hubiao
 * @since 2020-10-28 21:01
 */
@SpringBootApplication
@EnableDiscoveryClient
public class MerchantApplicationBootstrap {

    public static void main(String[] args) {
        SpringApplication.run( MerchantApplicationBootstrap.class, args );
        System.out.println( "启动成功！" );
    }

    /**
     * 将RestTemplate这种spring自带的请求客户端工具注入到蚂蚁应用项目中
     *
     * @return
     */
    @Bean
    RestTemplate getRestTemplate() {
        //RestTemplate(ClientHttpRequestFactory requestFactory)使用构造器返回一个RestTemplate
        // OkHttp3ClientHttpRequestFactory 是ClientHttpRequestFactory的一个实现类
        RestTemplate restTemplate = new RestTemplate( new OkHttp3ClientHttpRequestFactory() );
        //得到消息转换器
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        //指定String类型得消息转换器编码为UTF-8
        messageConverters.set( 1, new StringHttpMessageConverter( StandardCharsets.UTF_8 ) );
        return restTemplate;
    }
}
